<?php
if (file_exists("../config.php")) {
	include("../config.php");
	if (isset($_POST["webname"])) {
		$link = mysql_connect($HOST, $USER, $PASS) or die("Error al conectar a la DB");
		mysql_select_db($DB, $link) or die("Error al elegir la db"); 
		mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["webname"]."' WHERE param LIKE 'name'") or die (mysql_error());
		if (isset($_POST["webmail"]) && $_POST["webmail"]) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["webmail"]."' WHERE param LIKE 'contact_mail'") or die (mysql_error());
		if (isset($_POST["cookieprefix"]) && $_POST["cookieprefix"]) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["cookieprefix"]."' WHERE param LIKE 'cookie_prefix'") or die (mysql_error());
		
		if (isset($_POST["guestaccess"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["guestaccess"]."' WHERE param LIKE 'guest_access'") or die (mysql_error());
		if (isset($_POST["guestcomment"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["guestcomment"]."' WHERE param LIKE 'guest_comment'") or die (mysql_error());
		if (isset($_POST["guestdownload"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["guestdownload"]."' WHERE param LIKE 'guest_download'") or die (mysql_error());
		if (isset($_POST["guestvote"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["guestvote"]."'WHERE param LIKE 'guest_vote'") or die (mysql_error());
		if (isset($_POST["guestpost"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["guestpost"]."' WHERE param LIKE 'guest_post'") or die (mysql_error());
		if (isset($_POST["register"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["register"]."' WHERE param LIKE 'guest_register'") or die (mysql_error());
		if (isset($_POST["registerwithoutinv"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["registerwithoutinv"]."' WHERE param LIKE 'guest_register_without_inv'") or die (mysql_error());
		
		if (isset($_POST["usercomment"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["usercomment"]."' WHERE param LIKE 'user_comment'") or die (mysql_error());
		if (isset($_POST["userdownload"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["userdownload"]."' WHERE param LIKE 'user_download'") or die (mysql_error());
		if (isset($_POST["uservote"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["uservote"]."' WHERE param LIKE 'user_vote'") or die (mysql_error());
		if (isset($_POST["userpost"])) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["userpost"]."' WHERE param LIKE 'user_post'") or die (mysql_error());
		
		if (isset($_POST["seourl"]) && checkSEO()) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["seourl"]."' WHERE param LIKE 'seo_url'") or die (mysql_error());
		if (isset($_POST["webaccess"]) && checkSEO()) mysql_query("UPDATE ".$MYSQL_PREFIX."config SET value='".$_POST["webaccess"]."' WHERE param LIKE 'web_access'") or die (mysql_error());

		if (((isset($_POST["seourl"]) && $_POST["seourl"]) || (isset($_POST["webaccess"]) && !$_POST["webaccess"])) && checkSEO()) {
			$fp = fopen("../.htaccess","w");
			fwrite($fp, 'Options Indexes FollowSymLinks
			RewriteEngine On
			RewriteBase /
			');
			if ((isset($_POST["seourl"]) && $_POST["seourl"]) && checkSEO()) {
				fwrite($fp, '
				RewriteRule ^store/(.+)/*$ store.php?c=$1
				RewriteRule ^item/(.+)/*$ viewitem.php?i=$1
				RewriteRule ^profile/(.+)/*$ ucp/ucpviewprofile.php?u=$1
				RewriteRule ^comment/(.+)/*$ comment.php?i=$1
				');
			}
			if ((isset($_POST["webaccess"]) && $_POST["webaccess"] == -1) && checkSEO()) {
				fwrite($fp, '
				RewriteCond %{REQUEST_URI} !mantenimiento.html
				RewriteCond %{REQUEST_URI} !adm/*
				RewriteCond %{REQUEST_URI} !include/
				RewriteCond %{REQUEST_URI} !include/*
				RewriteCond %{REQUEST_URI} !lang/
				RewriteCond %{REQUEST_URI} !lang/*
				RewriteRule $ mantenimiento.html [R=302,L]');
				$fap = fopen("../mantenimiento.html","w+");
				if ($_POST["webaccesstext"]) $mantenimiento = $_POST["webaccesstext"];
				else $mantenimiento = _l("cms_webclosed");
				fwrite($fap, $mantenimiento);
				fclose($fap);
			}
			fclose($fp);
			header("location: index.php?stage=5");
		} 
		echo "<script>alert('Done! Successfully installed.'); location.href='../';</script>";	
	} else include("stage4_style.php");
} else die(header("location: index.php"));
?>